import DataCenter from "./DataCenter";
import GameMinoFactory from "./Game/GameMino";
import SevenBagSystem from "./Game/SevenBagSystem";
import Grid from "./Grid";
import PopUp from "./PopUp";
import GameOver from "./PopUps/GameOver";
import MyProfile from "./PopUps/MyProfile";
import ThemeManager from "./ThemeManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainUI extends cc.Component {

    @property(Grid)
    boardGrid: Grid = null;
    @property(cc.Node)
    boardRoot: cc.Node = null;

    @property(cc.Animation)
    main: cc.Animation = null;

    @property(cc.Animation)
    grid: cc.Animation = null;

    @property(cc.Animation)
    score: cc.Animation = null;

    @property(PopUp)
    settings: PopUp = null;

    @property(GameOver)
    gameover: GameOver = null;

    @property(MyProfile)
    myprofile: MyProfile = null;

    @property(cc.Node)
    restartNode: cc.Node = null;

    @property(cc.Label)
    currScore: cc.Label = null;
    @property(cc.Label)
    highScore: cc.Label = null;


    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        // 初始化数据中心
        ThemeManager.initThemeManager();
        DataCenter.initDataCenter();
        SevenBagSystem.initBagSystem();
        GameMinoFactory.initFactory();

        // 设置背景
        const layout = this.boardGrid.getComponent(cc.Layout);
        layout.spacingX = DataCenter.SPACING;
        layout.spacingY = DataCenter.SPACING;

        this.boardGrid.cols = DataCenter.Col;
        this.boardGrid.rows = DataCenter.Row;
        this.boardGrid.width = DataCenter.Width;

        this.scheduleOnce(() => {
            const pos = this.boardGrid.node.getPosition();
            const width = this.boardGrid.node.width;
            const height = this.boardGrid.node.height;
            
            console.log(`width: ${width}, height: ${height}`);

            this.boardRoot.setPosition(pos.x - width / 2, pos.y - height / 2);
            this.boardRoot.setContentSize(width, height);
        }, 0);


        this.updateHigh();
        this.hideRestart();
    }

    updateScore(score: number) {
        this.currScore.string = `${score}`;
    }

    updateHigh() {
        this.highScore.string = `${DataCenter.getHigh()}`;
    }

    hideRestart() {
        this.restartNode.active = false;
    }

    showRestart() {
        this.restartNode.active = true;
    }

    // update (dt) {}

    private _menuLoadedCallback = null;
    enterMenu(callback: () => void) {
        this._menuLoadedCallback = callback;
        this.showRestart();

        this.main.play('main-menu');
        this.grid.play('grid-menu');
        this.score.play('score-menu');
    }

    private _gameLoadedCallback = null;
    enterGame(callback: () => void) {
        this._gameLoadedCallback = callback;

        this.main.play('main-game');
        this.grid.play('grid-game');
        this.score.play('score-game');
    }

    frameEvent_onGameLoaded() {
        console.log(`frameEvent_onGameLoaded`);
        this._gameLoadedCallback && this._gameLoadedCallback();
    }

    frameEvent_OnMenuLoaded() {
        console.log(`frameEvent_OnMenuLoaded`);
        this._menuLoadedCallback && this._menuLoadedCallback();
    }
}
